<?php
/*
 * @Descripttion: 
 * @version: 1.0
 * @Author: Sam
 * @Date: 2022-03-10 20:16:59
 * @LastEditors: Sam
 * @LastEditTime: 2022-03-11 14:11:13
 */
namespace app\api\controller;
use app\__MODULE__\model\{Goods as GoodsModel,StoreCategory,GoodsAttr,GoodsCollection,GoodsComment,GoodsView};
use think\facade\Db;
use hg\apidoc\annotation as Apidoc;

/**
 * @Apidoc\Title("商品模块")
 */
class Goods extends Base{


    /**
     * @Apidoc\Title("商品分类")
     * @Apidoc\Url("/api/goods_category")
     * @Apidoc\Method("GET")
     * @Apidoc\Returned("[]", type="list[]",
	 * 	    @Apidoc\Returned("id", type="int",desc="分类id"),
	 * 		@Apidoc\Returned("cate_name", type="string",desc="分类名称"),
	 *	    @Apidoc\Returned("pic", type="string",desc="分类图片"),
	 * )
     */
	public function category()
    {
        $cateogry = StoreCategory::where('is_show', 1)->order('sort desc,id desc')->select();
        json_response(1,'success',arr_tree($cateogry->toArray(),true,0,['parent_key'=>'pid']));
    }

    /**
     * @Apidoc\Title("商品列表")
     * @Apidoc\Url("/api/goods_list")
     * @Apidoc\Method("GET")
     * @Apidoc\Returned("[]", type="list[]",
	 * 	    @Apidoc\Returned("goods_id", type="int",desc="商品id"),
	 * 		@Apidoc\Returned("image", type="string",desc="商品图片"),
	 *	    @Apidoc\Returned("store_name", type="string",desc="商品名称"),
	 *      @Apidoc\Returned("store_info", type="string",desc="商品简介"),
	 * 		@Apidoc\Returned("price", type="string",desc="商品价格"),
	 * 		@Apidoc\Returned("unit_name", type="string",desc="单位"),
	 *      @Apidoc\Returned("sales", type="string",desc="销量"),
	 *      @Apidoc\Returned("stock", type="string",desc="库存"),
	 *      @Apidoc\Returned("browse", type="string",desc="浏览量"),
	 * )
     */
    public function goods_list(){
    	$data = request()->param();
    	$where = [];
    	if(isset($data['cate_id']) && $data['cate_id'] !=''){
    		$where[] = ['cate_id','=',$data['cate_id']];
    	}
    	if(isset($data['keyword']) && $data['keyword'] !=''){
    		$where[] = ['keyword|store_name','like',"%{$data['keyword']}%"];
    	}
    	$page  = request()->param('page/d',1);
 		$limit = request()->param('limit/d',10);
    	$list  = GoodsModel::where('is_show',1)->where('is_del',0)->field('id as goods_id,image,store_name,store_info,price,unit_name,sales,stock,browse')->where($where)->order('is_hot desc,is_new desc,sales desc')->page($page,$limit)->select();
    	json_response(1,'success',$list);
    }

     /**
     * @Apidoc\Title("商品详情")
     * @Apidoc\Url("/api/goods_detail")
     * @Apidoc\Method("GET")
	 * @Apidoc\Param("goods_id", type="int",desc="商品id"),
     * @Apidoc\Returned("goods_info", type="object",desc="商品详情",
	 * 	    @Apidoc\Returned("id", type="int",desc="商品id"),
	 * 		@Apidoc\Returned("image", type="string",desc="商品图片"),
	 * 		@Apidoc\Returned("slider_image", type="[]",desc="商品轮播图"),
	 *	    @Apidoc\Returned("store_name", type="string",desc="商品名称"),
	 *      @Apidoc\Returned("store_info", type="string",desc="商品简介"),
	 *      @Apidoc\Returned("keyword", type="string",desc="商品关键字"),
	 * 		@Apidoc\Returned("price", type="string",desc="商品价格"),
	 * 		@Apidoc\Returned("unit_name", type="string",desc="单位"),
	 *      @Apidoc\Returned("sales", type="string",desc="销量"),
	 *      @Apidoc\Returned("stock", type="string",desc="库存"),
	 *      @Apidoc\Returned("browse", type="string",desc="浏览量"),
	 *      @Apidoc\Returned("description", type="text",desc="详情"),
	 *      @Apidoc\Returned("spec_type", type="int",desc="规则 0单规格 1多规格"),
	 *      @Apidoc\Returned("is_collection", type="int",desc="是否已收藏 0 未收藏 1已收藏"),
	 *      @Apidoc\Returned("all_comments", type="int",desc="评论量"),
	 * 		@Apidoc\Returned("comments", type="list[]",desc="评论列表",
	 * 					@Apidoc\Returned("comment_id", type="int",desc="评论ID"),
	 * 					@Apidoc\Returned("comments", type="string",desc="评论内容"),
	 * 					@Apidoc\Returned("images", type="list[]",desc="评论图片")
	 * 		)
	 * )
	* @Apidoc\Returned("productAttr", type="list[]",desc="商品规格显示",
	 * 	    @Apidoc\Returned("product_id", type="int",desc="商品id"),
	 * 		@Apidoc\Returned("attr_name", type="string",desc="规则名称"),
	 * 		@Apidoc\Returned("attr_values", type="list[]",desc="规格值"),
	 *	    @Apidoc\Returned("attr_value", type="list[]",desc="处理后的规格值 方面前端读取",
	 *					 @Apidoc\Returned("attr", type="string",desc="显示值"),
	 *					 @Apidoc\Returned("check", type="bool",desc="是否选中")
	 *		)
	 * )
	 *@Apidoc\Returned("productValue", type="list[]",desc="选择规格生成的商品列表 用户选择完成后生成作为后续传值",
	 * 				@Apidoc\Returned("product_id", type="int",desc="商品ID"),
	 * 				@Apidoc\Returned("stock", type="int",desc="库存"),
	 * 				@Apidoc\Returned("suk", type="string",desc="suk"),
	 * 				@Apidoc\Returned("image", type="string",desc="图片"),
	 * 				@Apidoc\Returned("unique", type="string",desc="商品唯一标识"),
	 * 				@Apidoc\Returned("price", type="string",desc="价格"),
	 * 				@Apidoc\Returned("ot_price", type="string",desc="原价")
	 * 		)
	 * 
     */
    public function detail(){
    	$goods_id = param_check('goods_id');
    	$uid      = 1;
    	$goods_info   = GoodsModel::where('is_show',1)->where('stock','>',0)->where('id',$goods_id)->field('id,image,slider_image,store_name,store_info,keyword,cate_id,price,ot_price,unit_name,sales,stock,spec_type,description')->find();
    	if(!$goods_info) json_response(0,'商品已下架或不存在');
    	list($productAttr, $productValue) = GoodsAttr::getProductAttrDetail($goods_id);
        if (!$goods_info['spec_type']) {
            $productAttr  = [];
            $productValue = [];
        }
		$goods_info->is_collection = GoodsCollection::where(['uid'=>$uid,'goods_id'=>$goods_id])->count() ? true :false;
		$goods_info->comments      = GoodsComment::where(['uid'=>$uid,'goods_id'=>$goods_id,'is_show'=>1,'status'=>1])->field('id as comment_id,uid,goods_id,comments,images,nickname,avatar')->order('is_top desc,id desc')->limit(3)->select();
		$goods_info->all_comments   = GoodsComment::where('goods_id',$goods_id)->where('status',1)->where('is_show',1)->count();
		$goods_info->browse +=1;
		$goods_info->save();
		if(!GoodsView::where(['uid'=>$uid,'goods_id'=>$goods_id])->count()){
			GoodsView::create([
				'uid' => $uid,
				'goods_id' => $goods_id
			]);
		}
    	json_response(1,'success',compact('goods_info','productAttr','productValue'));
    }
}